#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QString>
#include <QThread>

using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //载入按钮图片
    QIcon con(":/picture/picture/back.png");
    QIcon con0(":/picture/picture/0.png");
    QIcon con1(":/picture/picture/1.png");
    QIcon con2(":/picture/picture/2.png");
    QIcon con3(":/picture/picture/3.png");
    QIcon con4(":/picture/picture/4.png");
    QIcon con5(":/picture/picture/5.png");
    QIcon con6(":/picture/picture/6.png");
    QIcon con7(":/picture/picture/7.png");
    QIcon con8(":/picture/picture/8.png");
    QIcon con9(":/picture/picture/9.png");
    QIcon conjia(":/picture/picture/jia.png");
    QIcon conjian(":/picture/picture/jian.png");
    QIcon concheng(":/picture/picture/cheng.png");
    QIcon conchu(":/picture/picture/chu.png");
    QIcon conleft(":/picture/picture/left.png");
    QIcon conright(":/picture/picture/right.png");
    QIcon condian(":/picture/picture/d.png");
    QIcon conclear(":/picture/picture/clear.png");
    QIcon condeng(":/picture/picture/deng.png");

    //放置按钮图片
//    ui->delButton->setIcon(con);
//    ui->zeroButton->setIcon(con0);
//    ui->oneButton->setIcon(con1);
//    ui->twoButton->setIcon(con2);
//    ui->threeButton->setIcon(con3);
//    ui->fourButton->setIcon(con4);
//    ui->fiveButton->setIcon(con5);
//    ui->sixButton->setIcon(con6);
//    ui->sevenButton->setIcon(con7);
//    ui->eightButton->setIcon(con8);
//    ui->nineButton->setIcon(con9);
//    ui->addButton->setIcon(conjia);
//    ui->subButton->setIcon(conjian);
//    ui->mulButton->setIcon(concheng);
//    ui->divButton->setIcon(conchu);
//    ui->leftButton->setIcon(conleft);
//    ui->rightButton->setIcon(conright);
//    ui->pointButton->setIcon(condian);
//    ui->cleButton->setIcon(conclear);
//    ui->equButton->setIcon(condeng);

    //载入音效
    startSound = new QMediaPlayer[120];
    startSound[0].setMedia(QUrl("qrc:/sound/sound/0.wav"));
    startSound[1].setMedia(QUrl("qrc:/sound/sound/1.wav"));
    startSound[2].setMedia(QUrl("qrc:/sound/sound/2.wav"));
    startSound[3].setMedia(QUrl("qrc:/sound/sound/3.wav"));
    startSound[4].setMedia(QUrl("qrc:/sound/sound/4.wav"));
    startSound[5].setMedia(QUrl("qrc:/sound/sound/5.wav"));
    startSound[6].setMedia(QUrl("qrc:/sound/sound/6.wav"));
    startSound[7].setMedia(QUrl("qrc:/sound/sound/7.wav"));
    startSound[8].setMedia(QUrl("qrc:/sound/sound/8.wav"));
    startSound[9].setMedia(QUrl("qrc:/sound/sound/9.wav"));
    startSound[10].setMedia(QUrl("qrc:/sound/sound/jia.wav"));
    startSound[11].setMedia(QUrl("qrc:/sound/sound/jian.wav"));
    startSound[12].setMedia(QUrl("qrc:/sound/sound/cheng.wav"));
    startSound[13].setMedia(QUrl("qrc:/sound/sound/chu.wav"));
    startSound[14].setMedia(QUrl("qrc:/sound/sound/deng.wav"));
    startSound[15].setMedia(QUrl("qrc:/sound/sound/ce.wav"));
    startSound[16].setMedia(QUrl("qrc:/sound/sound/sin.wav"));
    startSound[17].setMedia(QUrl("qrc:/sound/sound/cos.wav"));
    startSound[18].setMedia(QUrl("qrc:/sound/sound/tan.wav"));
    startSound[19].setMedia(QUrl("qrc:/sound/sound/sqrt.wav"));
    startSound[20].setMedia(QUrl("qrc:/sound/sound/dian.wav"));
    startSound[21].setMedia(QUrl("qrc:/sound/sound/x2.wav"));
    startSound->setVolume(voice);

    //设置窗口标题
    this->setWindowTitle("计算器");

    //设置字体
    QFont f("仿宋",20);
    ui->mainLineEdit->setFont(f);

    //设置背景图片
    QPalette pal;
        pal.setBrush(QPalette::Background,QBrush(QPixmap(":/picture/bk_4.jpg")));
        this->setPalette(pal);
    //this->setStyleSheet("background-color: rgb(53,181,185);");
}

Widget::~Widget()
{
    delete ui;
}

//键盘输入功能
void Widget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_0:
        expression+="0";
        realexp+='0';
        ui->mainLineEdit->setText(expression);
        startSound[0].play();
        break;
    case Qt::Key_1:
        expression+="1";
        realexp+='1';
        ui->mainLineEdit->setText(expression);
        startSound[1].play();
        break;
    case Qt::Key_2:
        expression+="2";
        realexp+='2';
        ui->mainLineEdit->setText(expression);
        startSound[2].play();
        break;
    case Qt::Key_3:
        expression+="3";
        realexp+='3';
        ui->mainLineEdit->setText(expression);
        startSound[3].play();
        break;
    case Qt::Key_4:
        expression+="4";
        realexp+='4';
        ui->mainLineEdit->setText(expression);
        startSound[4].play();
        break;
    case Qt::Key_5:
        expression+="5";
        realexp+='5';
        ui->mainLineEdit->setText(expression);
        startSound[5].play();
        break;
    case Qt::Key_6:
        expression+="6";
        realexp+='6';
        ui->mainLineEdit->setText(expression);
        startSound[6].play();
        break;
    case Qt::Key_7:
        expression+="7";
        realexp+='7';
        ui->mainLineEdit->setText(expression);
        startSound[7].play();
        break;
    case Qt::Key_8:
        expression+="8";
        realexp+='8';
        ui->mainLineEdit->setText(expression);
        startSound[8].play();
        break;
    case Qt::Key_9:
        expression+="9";
        realexp+='9';
        ui->mainLineEdit->setText(expression);
        startSound[9].play();
        break;
    case Qt::Key_Plus:
    {
        QString t="";
        if(expression==t){
            //void
        }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
                realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
            expression[expression.length()-1]='+';
            realexp[realexp.length()-1]='+';
        }else{
            expression+="+";
            realexp+='+';
        }
        ui->mainLineEdit->setText(expression);
        startSound[10].play();
    }
        break;
    case Qt::Key_Minus:
    {
        QString t="";
        if(expression==t){
            expression+="-";
            realexp+="-";
        }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
                realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
            expression[expression.length()-1]='-';
            realexp[realexp.length()-1]='-';
        }else{
            expression+="-";
            realexp+='-';
        }
        ui->mainLineEdit->setText(expression);
        startSound[11].play();
    }
        break;
    case Qt::Key_Asterisk:
    {
        QString t="";
        if(expression==t){
            //void
        }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
                realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
            expression[expression.length()-1]='*';
            realexp[realexp.length()-1]='*';
        }else{
            expression+="*";
            realexp+='*';
        }
        ui->mainLineEdit->setText(expression);
        startSound[12].play();
    }
        break;
    case Qt::Key_Slash :
    {
        QString t="";
        if(expression==t){
            //void
        }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
                realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
            expression[expression.length()-1]='/';
            realexp[realexp.length()-1]='/';
        }else{
            expression+="/";
            realexp+='/';
        }
        ui->mainLineEdit->setText(expression);
        startSound[13].play();
    }
        break;
    case Qt::Key_ParenLeft :
    {
        QString t="";
        if(expression==t){
            expression+="(";
            realexp+="(";
        }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
            expression+="*(";
            realexp+="*(";
        }else{
                expression+="(";
                realexp+="(";
        }
        ui->mainLineEdit->setText(expression);
    }
        break;
    case Qt::Key_ParenRight :
    {
        QString t="";
        if(expression==t){
            //void
        }else{
            leftNum=0;rightNum=0;
            for(int i=0;i<expression.length();i++){
                if(expression[i]=="("){
                    leftNum++;
                }else if(expression[i]==")"){
                    rightNum++;
                }
            }
            if(leftNum>rightNum){
                expression+=')';
                realexp+=')';
            }
        }
        ui->mainLineEdit->setText(expression);
    }
        break;
    case Qt::Key_Equal :
    case Qt::Key_Enter :
    {
        startSound[14].play();
        leftNum=0;rightNum=0;
        bool haveNum=false;//左右括号间是否有数
        for(int i=1;i<realexp.length();i++){
            if(realexp[i]==')'&&realexp[i-1]!=')'&&realexp[i-1]!='.'
                    &&!(realexp[i-1]>='0'&&realexp[i-1]<='9')&&realexp[i-1]!='!'){
                isError=true;
            }
        }
        if(realexp[realexp.length()-1]!=')'&&realexp[realexp.length()-1]!='!'    //判断是否以运算符结尾
                &&!(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9')){
            isError=true;
        }
        if(canEqu){
            if(!isError){
                for(int i=0;i<realexp.length();i++){//统计左右括号数
                    if(realexp[i]=="("){
                        leftNum++;
                    }else if(realexp[i]==")"){
                        rightNum++;
                    }
                }
                for(;leftNum>rightNum;rightNum++){//补全右括号
                    expression+=')';
                    realexp+=')';
                }
                for(int i=0;i<realexp.length();i++){
                    if(realexp[realexp.length()-1-i]>='0'&&realexp[realexp.length()-1-i]<='9'){
                        haveNum=true;
                    }else if(realexp[realexp.length()-1-i]=='('){
                        break;
                    }
                }
            }else{
                expression="输入错误！";
                realexp="输入错误！";
            }
            if(haveNum){
                double result=cal(realexp);
                expression=QString::asprintf("%g",result);
                realexp=QString::asprintf("%f",result);
            }else{
                expression="输入错误！";
                realexp="输入错误！";
                canEqu=false;
            }
            ui->mainLineEdit->setText(expression);
        }
    }
        break;
    case Qt::Key_Backspace :
        canEqu=true;
        isError=false;
        expression.chop(1);
        realexp.chop(1);
        ui->mainLineEdit->setText(expression);
        break;
    case Qt::Key_Period :
    {
        bool havepot=false;
        for(int i=0;i<realexp.length();i++){
            if(realexp[realexp.length()-1-i]=='.'){
                havepot=true;
                break;
            }
            if((!(realexp[realexp.length()-1-i]>='0'&&realexp[realexp.length()-1-i]<='9'))){
                break;
            }
        }
        if(!havepot){
            expression+='.';
            realexp+='.';
        }
        ui->mainLineEdit->setText(expression);
    }
        break;
    }
}


void Widget::on_oneButton_clicked()
{
    expression+="1";
    realexp+='1';
    ui->mainLineEdit->setText(expression);
    startSound[1].play();
}

void Widget::on_twoButton_clicked()
{
    expression+="2";
    realexp+='2';
    ui->mainLineEdit->setText(expression);
    startSound[2].play();
}

void Widget::on_threeButton_clicked()
{
    expression+="3";
    realexp+='3';
    ui->mainLineEdit->setText(expression);
    startSound[3].play();
}

void Widget::on_fourButton_clicked()
{
    expression+="4";
    realexp+='4';
    ui->mainLineEdit->setText(expression);
    startSound[4].play();
}

void Widget::on_fiveButton_clicked()
{
    expression+="5";
    realexp+='5';
    ui->mainLineEdit->setText(expression);
    startSound[5].play();
}

void Widget::on_sixButton_clicked()
{
    expression+="6";
    realexp+='6';
    ui->mainLineEdit->setText(expression);
    startSound[6].play();
}

void Widget::on_sevenButton_clicked()
{
    expression+="7";
    realexp+='7';
    ui->mainLineEdit->setText(expression);
    startSound[7].play();
}

void Widget::on_eightButton_clicked()
{
    expression+="8";
    realexp+='8';
    ui->mainLineEdit->setText(expression);
    startSound[8].play();
}

void Widget::on_nineButton_clicked()
{
    expression+="9";
    realexp+='9';
    ui->mainLineEdit->setText(expression);
    startSound[9].play();
}

void Widget::on_zeroButton_clicked()
{
    expression+="0";
    realexp+='0';
    ui->mainLineEdit->setText(expression);
    startSound[0].play();
}

void Widget::on_leftButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="(";
        realexp+="(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*(";
        realexp+="*(";
    }else{
            expression+="(";
            realexp+="(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_rightButton_clicked()
{
    QString t="";
    if(expression==t){
        //void
    }else{
        leftNum=0;rightNum=0;
        for(int i=0;i<expression.length();i++){
            if(expression[i]=="("){
                leftNum++;
            }else if(expression[i]==")"){
                rightNum++;
            }
        }
        if(leftNum>rightNum){
            expression+=')';
            realexp+=')';
        }
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_divButton_clicked()
{
    QString t="";
    if(expression==t){
        //void
    }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
            realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
        expression[expression.length()-1]='/';
        realexp[realexp.length()-1]='/';
    }else{
        expression+="/";
        realexp+='/';
    }
    ui->mainLineEdit->setText(expression);
    startSound[13].play();
}

void Widget::on_mulButton_clicked()
{
    QString t="";
    if(expression==t){
        //void
    }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
            realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
        expression[expression.length()-1]='*';
        realexp[realexp.length()-1]='*';
    }else{
        expression+="*";
        realexp+='*';
    }
    ui->mainLineEdit->setText(expression);
    startSound[12].play();
}

void Widget::on_subButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="-";
        realexp+="-";
    }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
            realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
        expression[expression.length()-1]='-';
        realexp[realexp.length()-1]='-';
    }else{
        expression+="-";
        realexp+='-';
    }
    ui->mainLineEdit->setText(expression);
    startSound[11].play();
}

void Widget::on_addButton_clicked()
{
    QString t="";
    if(expression==t){
        //void
    }else if(realexp[realexp.length()-1]=='+'||realexp[realexp.length()-1]=='-'||
            realexp[realexp.length()-1]=='*'||realexp[realexp.length()-1]=='/'){
        expression[expression.length()-1]='+';
        realexp[realexp.length()-1]='+';
    }else{
        expression+="+";
        realexp+='+';
    }
    ui->mainLineEdit->setText(expression);
    startSound[10].play();
}

void Widget::on_cleButton_clicked()
{
    canEqu=true;
    isError=false;
    startSound[15].play();
    expression.clear();
    realexp.clear();
    ui->mainLineEdit->setText(expression);
}

void Widget::on_delButton_clicked()
{
    canEqu=true;
    isError=false;
    expression.chop(1);
    realexp.chop(1);
    ui->mainLineEdit->setText(expression);
}

void Widget::on_equButton_clicked()
{
    startSound[14].play();
    leftNum=0;rightNum=0;
    bool haveNum=false;//左右括号间是否有数
    QString t="";
    if(expression==t||realexp==t){
        return;
    }
    for(int i=1;i<realexp.length();i++){
        if(realexp[i]==')'&&realexp[i-1]!=')'&&realexp[i-1]!='.'
                &&!(realexp[i-1]>='0'&&realexp[i-1]<='9')&&realexp[i-1]!='!'){
            isError=true;
        }
    }
    if(realexp[realexp.length()-1]!=')'&&realexp[realexp.length()-1]!='!'    //判断是否以运算符结尾
            &&!(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9')){
        isError=true;
    }
    if(canEqu){
        if(!isError){
            for(int i=0;i<realexp.length();i++){//统计左右括号数
                if(realexp[i]=="("){
                    leftNum++;
                }else if(realexp[i]==")"){
                    rightNum++;
                }
            }
            for(;leftNum>rightNum;rightNum++){//补全右括号
                expression+=')';
                realexp+=')';
            }
            for(int i=0;i<realexp.length();i++){
                if(realexp[realexp.length()-1-i]>='0'&&realexp[realexp.length()-1-i]<='9'){
                    haveNum=true;
                }else if(realexp[realexp.length()-1-i]=='('){
                    break;
                }
            }
        }else{
            expression="输入错误！";
            realexp="输入错误！";
        }
        if(haveNum){
            double result=cal(realexp);
            expression=QString::asprintf("%g",result);
            realexp=QString::asprintf("%f",result);
        }else{
            expression="输入错误！";
            realexp="输入错误！";
            canEqu=false;
        }
        ui->mainLineEdit->setText(expression);
    }
}

void Widget::on_pointButton_clicked()
{
    startSound[20].play();
    bool havepot=false;
    for(int i=0;i<realexp.length();i++){
        if(realexp[realexp.length()-1-i]=='.'){
            havepot=true;
            break;
        }
        if((!(realexp[realexp.length()-1-i]>='0'&&realexp[realexp.length()-1-i]<='9'))){
            break;
        }
    }
    if(!havepot){
        expression+='.';
        realexp+='.';
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_sinButton_clicked()
{
    startSound[16].play();
    QString t="";
    if(expression==t){
        expression+="sin(";
        realexp+="s(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*sin(";
        realexp+="*s(";
    }else{
            expression+="sin(";
            realexp+="s(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_cosButton_clicked()
{
    startSound[17].play();
    QString t="";
    if(expression==t){
        expression+="cos(";
        realexp+="c(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*cos(";
        realexp+="*c(";
    }else{
            expression+="cos(";
            realexp+="c(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_tanButton_clicked()
{
    startSound[18].play();
    QString t="";
    if(expression==t){
        expression+="tan(";
        realexp+="t(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*tan(";
        realexp+="*t(";
    }else{
            expression+="tan(";
            realexp+="t(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_arcsinButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="arcsin(";
        realexp+="a(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*arcsin(";
        realexp+="*a(";
    }else{
            expression+="arcsin(";
            realexp+="a(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_arccosButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="arccos(";
        realexp+="b(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*arccos(";
        realexp+="*b(";
    }else{
            expression+="arccos(";
            realexp+="b(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_arctanButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="arctan(";
        realexp+="d(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*arctan(";
        realexp+="*d(";
    }else{
            expression+="arctan(";
            realexp+="d(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_InButton_clicked()
{
    QString t="";
    if(expression==t){
        expression+="In(";
        realexp+="I(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*In(";
        realexp+="*I(";
    }else{
            expression+="In(";
            realexp+="I(";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_miButton_clicked()
{
    expression+="^";
    realexp+='^';
    ui->mainLineEdit->setText(expression);
}

void Widget::on_PIButton_clicked()
{
    QString t="";
    if(realexp==t){
        expression+="π";
        realexp+="3.1415926";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*π";
        realexp+="*3.1415926";
    }else{
            expression+="π";
            realexp+="3.1415926";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_eButton_clicked()
{
    QString t="";
    if(realexp==t){
        expression+="e";
        realexp+="2.7182818";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*e";
        realexp+="*2.7182818";
    }else{
            expression+="e";
            realexp+="2.7182818";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_sqrt_clicked()
{
    startSound[19].play();
    QString t="";
    if(expression==t){
        expression+="√(";
        realexp+="_(";
    }else if(realexp[realexp.length()-1]>='0'&&realexp[realexp.length()-1]<='9'){
        expression+="*√";
        realexp+="*_";
    }else{
            expression+="√";
            realexp+="_";
    }
    ui->mainLineEdit->setText(expression);
}

void Widget::on_dButton_clicked()
{
    startSound[21].play();
    bool isLegal=true;
    for(int i=0;i<realexp.length();i++){
        if((!(realexp[i]>='0'&&realexp[i]<='9'))&&realexp[i]!='.'
                &&realexp[i]!='('&&realexp[i]!=')'){
            isLegal=false;
        }
    }
    if(isLegal){
        leftNum=0;rightNum=0;
        for(int i=0;i<realexp.length();i++){
            if(realexp[i]=="("){
                leftNum++;
            }else if(realexp[i]==")"){
                rightNum++;
            }
        }
        while(leftNum>rightNum){
            expression+=")";
            realexp+=")";
            rightNum++;
        }
        realexp+="^2";
        double result=cal(realexp);
        expression=QString::asprintf("%g",result);
        realexp=QString::asprintf("%f",result);
        ui->mainLineEdit->setText(expression);
    }
}

void Widget::on_factoricalButton_clicked()
{
    expression+="!";
    realexp+="!";
    ui->mainLineEdit->setText(expression);
}
